library(leaflet)
# library(leaflet.extras)
library(leafletCN)

# Example----
m <- leaflet() %>%
  amap() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng = 121.48024, lat = 31.23631, popup = "Shanghai")

m  # Print the map

# View control----
m = leaflet() %>% addTiles() %>% 
  setView(-71.0382679, 42.3489054, zoom = 18)
m %>% fitBounds(-72, 40, -70, 43)
m %>% setMaxBounds(-72, 40, -70, 43)
m %>% clearBounds()  # World view

# leaflet----
df = data.frame(lat = 1:10, long = rnorm(10))
leaflet(df) %>% addCircles()
leaflet(df) %>% addCircles(lng = ~long, lat = ~lat)
leaflet(df) %>% addCircles(data = df)
leaflet(df) %>% addCircles(data = df, lng = ~long, lat = ~lat)

# sp----
library(sp)

Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
Sr3 = Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5)))
Sr4 = Polygon(cbind(c(5, 6, 6, 5, 5), c(4, 4, 3, 3, 4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr4, Sr3), "s3/4")
SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3)
leaflet(height = "300px") %>% addPolygons(data = SpP)

# map----
library(maps)
mapStates = map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
  addPolygons(fillColor = topo.colors(10, alpha = NULL), 
              stroke = FALSE)

# Tile layer----
# open street map
leaflet() %>% addTiles()

# GaoDe map (leafletCN)
leaflet() %>% amap()

# Dark style
leaflet() %>% addProviderTiles("Thunderforest.spinalMap")

# Satellite map
leaflet() %>% addProviderTiles("Esri.worldImagery")
